package com.goktech.olala.core.service.impl;

import com.goktech.olala.core.req.ReqGoods;
import com.goktech.olala.core.resp.RespGoodsVo;
import com.goktech.olala.core.service.IQueryGoodService;
import com.goktech.olala.server.dao.goods.GoodMapper;
import com.goktech.olala.utils.RMBUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author sanming
 * @Description 商品查找
 * @Date 2020/12/11 15:49
 */

@Service
public class QueryGoodServiceImp implements IQueryGoodService {

    @Autowired
    GoodMapper goodMapper;

    /**
     * 商品查询
     * @param goodsReq
     * @return
     * @throws Exception
     */
    @Override
    public List<RespGoodsVo> queryGoodsByParam(ReqGoods goodsReq) throws Exception {
        List<RespGoodsVo> respGoodsVosList = goodMapper.selectByExample(goodsReq);
        if(respGoodsVosList != null){
            for (RespGoodsVo respGoodsVo: respGoodsVosList) {
                //将分为单位的转换为元并返回金额格式的字符串
                respGoodsVo.setMarketPrice(RMBUtil.changeF2Y(Long.valueOf(respGoodsVo.getMarketPrice())));
                respGoodsVo.setShopPrice(RMBUtil.changeF2Y(Long.valueOf(respGoodsVo.getShopPrice())));
                respGoodsVo.setCostPrice(RMBUtil.changeF2Y(Long.valueOf(respGoodsVo.getCostPrice())));
                respGoodsVo.setPromotePrice(RMBUtil.changeF2Y(Long.valueOf(respGoodsVo.getPromotePrice())));
                respGoodsVo.setShipFee(RMBUtil.changeF2Y(Long.valueOf(respGoodsVo.getShipFee())));
            }
        }
        return respGoodsVosList;
    }

    /**
     * 商品详情功能
     * @param goodsId
     * @return
     * @throws Exception
     */
    @Override
    public RespGoodsVo selectByPrimaryKey(String goodsId) throws Exception{
        RespGoodsVo respGoodsVo = goodMapper.selectByPrimaryKey(goodsId);
        respGoodsVo.setMarketPrice(RMBUtil.changeF2Y(Long.valueOf(respGoodsVo.getMarketPrice())));
        respGoodsVo.setShopPrice(RMBUtil.changeF2Y(Long.valueOf(respGoodsVo.getShopPrice())));
        respGoodsVo.setCostPrice(RMBUtil.changeF2Y(Long.valueOf(respGoodsVo.getCostPrice())));
        respGoodsVo.setPromotePrice(RMBUtil.changeF2Y(Long.valueOf(respGoodsVo.getPromotePrice())));
        return respGoodsVo;
    }

}
